<template>
	<div>
		<!-- 开票提示窗 -->
		<uni-popup ref="invoiceDialog" type="dialog">
			<uni-popup-dialog type="info" cancelText="关闭" confirmText="确认开票" title="录入发票"
				@confirm="invoiceDialogConfirm" :before-close="true" @close="closeDialog('invoiceDialog')">
				<scroll-view scroll-top="0" scroll-y="true" style="height: 300px;">
					<uni-forms ref="form" :modelValue="formData" :rules="rules" labelWidth="75px">
						<uni-forms-item label="公司名称" name="companyName" required>
							<uni-easyinput type="text" v-model="formData.companyName" placeholder="请输入公司名称" />
						</uni-forms-item>
						<uni-forms-item label="公司税号" name="companyTaxNo" required>
							<uni-easyinput type="text" v-model="formData.companyTaxNo" placeholder="请输入公司税号" />
						</uni-forms-item>
						<!-- <uni-forms-item label="发票类型" name="type" required>
							<uni-data-select  v-model="formData.type" :localdata="type"
								style="background-color: white;" />
						</uni-forms-item> -->
						<uni-forms-item label="公司电话" name="companyMobile">
							<uni-easyinput type="text" v-model="formData.companyMobile" placeholder="请输入公司电话" />
						</uni-forms-item>
						<uni-forms-item label="公司地址" name="companyAddress">
							<uni-easyinput type="text" v-model="formData.companyAddress" placeholder="请输入公司地址" />
						</uni-forms-item>
						<uni-forms-item label="公司开户行" name="accountBank">
							<uni-easyinput type="text" v-model="formData.accountBank" placeholder="请输入公司开户行" />
						</uni-forms-item>
						<uni-forms-item label="公司行账号" name="accountNumber">
							<uni-easyinput type="text" v-model="formData.accountNumber" placeholder="请输入公司行账号" />
						</uni-forms-item>
						<uni-forms-item label="电子邮箱" name="email">
							<uni-easyinput type="text" v-model="formData.email" placeholder="请输入电子邮箱" />
						</uni-forms-item>
						<uni-forms-item label="总金额" name="totalAmount">
							<uni-easyinput disabled type="text" v-model="formData.totalAmount" placeholder="请输入总金额" />
						</uni-forms-item>
						<uni-forms-item label="发票内容" name="invoiceContent">
							<uni-easyinput disabled type="text" v-model="formData.invoiceContent" placeholder="请输入发票内容" />
						</uni-forms-item>
						<uni-forms-item label="发票方" name="invoiceParty">
							<uni-easyinput disabled type="text" v-model="formData.invoiceParty" placeholder="请输入发票方" />
						</uni-forms-item>
						<uni-forms-item label="发票总额" name="invoiceTotal">
							<uni-easyinput disabled type="text" v-model="formData.invoiceTotal" placeholder="请输入发票总额" />
						</uni-forms-item>
						<!-- <uni-forms-item label="发票数量" name="quantity" required>
							<uni-easyinput type="text" v-model="formData.quantity" placeholder="请输入发票数量" />
						</uni-forms-item> -->
						<uni-forms-item label="备注">
							<uni-easyinput type="textarea" v-model="formData.remark" placeholder="相关备注说明" />
						</uni-forms-item>
					</uni-forms>
					<view style="height: 50px;"></view>
				</scroll-view>
			</uni-popup-dialog>
		</uni-popup>
		<!-- 重新开票提示窗 -->
		<uni-popup ref="cancelInvoiceDialog" type="dialog">
			<uni-popup-dialog type="info" cancelText="关闭" confirmText="确认" title="重新开票"
				@confirm="cancelInvoiceDialogConfirm">
				<scroll-view scroll-top="0" scroll-y="true" style="height: 300px;">
					<uni-forms ref="form_cancle" :modelValue="formData_cancel" :rules="rules_cancel" labelWidth="75px">
						<uni-forms-item label="公司名称" name="companyName" required>
							<uni-easyinput type="text" v-model="formData_cancel.companyName" placeholder="请输入公司名称" />
						</uni-forms-item>
						<uni-forms-item label="公司税号" name="companyTaxNo" required>
							<uni-easyinput type="text" v-model="formData_cancel.companyTaxNo" placeholder="请输入公司税号" />
						</uni-forms-item>
						<!-- <uni-forms-item label="发票类型" name="type" required>
							<uni-data-select  v-model="formData_cancel.type" :localdata="type"
								style="background-color: white;" />
						</uni-forms-item> -->
						<uni-forms-item label="公司电话" name="companyMobile">
							<uni-easyinput type="text" v-model="formData_cancel.companyMobile" placeholder="请输入公司电话" />
						</uni-forms-item>
						<uni-forms-item label="公司地址" name="companyAddress">
							<uni-easyinput type="text" v-model="formData_cancel.companyAddress" placeholder="请输入公司地址" />
						</uni-forms-item>
						<uni-forms-item label="公司开户行" name="accountBank">
							<uni-easyinput type="text" v-model="formData_cancel.accountBank" placeholder="请输入公司开户行" />
						</uni-forms-item>
						<uni-forms-item label="公司行账号" name="accountNumber">
							<uni-easyinput type="text" v-model="formData_cancel.accountNumber" placeholder="请输入公司行账号" />
						</uni-forms-item>
						<uni-forms-item label="电子邮箱" name="email">
							<uni-easyinput type="text" v-model="formData_cancel.email" placeholder="请输入电子邮箱" />
						</uni-forms-item>
						<uni-forms-item label="总金额" name="totalAmount">
							<uni-easyinput disabled type="text" v-model="formData_cancel.totalAmount" placeholder="请输入总金额" />
						</uni-forms-item>
						<uni-forms-item label="发票内容" name="invoiceContent">
							<uni-easyinput disabled type="text" v-model="formData_cancel.invoiceContent" placeholder="请输入发票内容" />
						</uni-forms-item>
						<uni-forms-item label="发票方" name="invoiceParty">
							<uni-easyinput disabled type="text" v-model="formData_cancel.invoiceParty" placeholder="请输入发票方" />
						</uni-forms-item>
						<uni-forms-item label="发票总额" name="invoiceTotal">
							<uni-easyinput disabled type="text" v-model="formData_cancel.invoiceTotal" placeholder="请输入发票总额" />
						</uni-forms-item>
						<!-- <uni-forms-item label="发票数量" name="quantity" required>
							<uni-easyinput type="text" v-model="formData_cancel.quantity" placeholder="请输入发票数量" />
						</uni-forms-item> -->
						<uni-forms-item label="备注">
							<uni-easyinput type="textarea" v-model="formData_cancel.remark" placeholder="相关备注说明" />
						</uni-forms-item>
					</uni-forms>
					<view style="height: 50px;"></view>
				</scroll-view>
			</uni-popup-dialog>
		</uni-popup>
	</div>
</template>

<script>
	import{
		invoiceAdd,
		reInvoice
	} from '@/api/system/invoice.js'
	import {getDicts} from '@/api/system/dict.js'
	export default {
		data() {
			return {
				//开票
				formData:{
					companyName: '',
					companyTaxNo: '',
					companyAddress: '',
					type:'',
					invoiceTotal:'',
					quantity:'',
					invoiceContent:'物流运输',
					invoiceParty:'',
					companyMobile: '',
					accountBank: '',
					accountNumber: '',
					totalAmount: '',
					email:'',
					remark:''
				},
				// 校验规则->开票
				rules: {
					companyName: {
						rules: [{
							required: true,
							errorMessage: '请输入公司名称',
						}]
					},
					companyTaxNo: {
						rules: [{
							required: true,
							errorMessage: '请输入公司税号',
						}]
					},
					// type:{
					// 	rules: [{
					// 		required: true,
					// 		errorMessage: '请选择发票类型',
					// 	}]
					// },
					// quantity:{
					// 	rules: [{
					// 		required: true,
					// 		errorMessage: '请输入发票数量',
					// 	}]
					// },
				},
				formData_cancel:{},
				// 校验规则->开票
				rules_cancel: {
					companyName: {
						rules: [{
							required: true,
							errorMessage: '请输入公司名称',
						}]
					},
					companyTaxNo: {
						rules: [{
							required: true,
							errorMessage: '请输入公司税号',
						}]
					},
					// type:{
					// 	rules: [{
					// 		required: true,
					// 		errorMessage: '请选择发票类型',
					// 	}]
					// },
					// quantity:{
					// 	rules: [{
					// 		required: true,
					// 		errorMessage: '请输入发票数量',
					// 	}]
					// },
				},
				//下拉框 ->发票类型
				type:[],
				user:uni.getStorageSync('storage_data').vuex_userInfo||[],
			}
		},
		created() {
			//加载发票类型
			getDicts('invoice_type').then(res=>{
				this.type=res.data.map(item=>{
					return {
						value:item.dictValue,
						text:item.dictLabel
					}
				})
			})
			
			//加载公司名称（发货方）
			this.formData.invoiceParty= this.user.dept.deptName
		},
		methods: {
			//关闭弹窗
			closeDialog(name){
				this.$refs[name].close()
			},
			//开票
			invoiceDialog(row){
				//默认发票类型为：电子发票 ，发票数量：1
				this.formData.type=0
				this.formData.quantity=1
				
				//  2==>'到付'
				if(row.settlementMode=='2')  //如果结算方式为“到付”，则显示收货方，其余显示发货方
				{
				  this.formData.companyName = row.deliveryName //公司名称（收货方）
				  this.formData.companyAddress = row.deliveryDistrictName+''+row.deliveryAddress //公司地址（收货方）
				  this.formData.companyMobile = row.deliveryContactTel //公司电话（收货方）
				  //this.formData.invoiceParty=row.deliveryName //公司名称（收货方）
				  this.formData.customerId=row.deliveryId //公司id（收货方）
				}
				else
				{
				  this.formData.companyName = row.shipName //公司名称（发货方）
				  this.formData.companyAddress = row.shipDistrictName+''+row.shipAddress //公司地址（发货方）
				  this.formData.companyMobile = row.shipContactTel //公司电话（发货方）
				  //this.formData.invoiceParty= row.shipName //公司名称（发货方）
				  this.formData.customerId=row.shipId //公司id（发货方）
				}
				this.formData.waybillId= row.waybillId  //运单ID
				this.getTotalMoney(row)  //计算总价
				this.$refs.invoiceDialog.open()
			},
			getTotalMoney(a){
			    const totalCarriageFee = +a.totalCarriageFee || 0
			    const totalPackingFee = +a.totalPackingFee || 0
			    const shipPickUpFee = +a.shipPickUpFee || 0
			    const deliveryPickUpFee = +a.deliveryPickUpFee || 0
		
			    const parcelInsuranceFee = +a.waybillValueAdd.parcelInsuranceFee || 0
			    const receiptFee = +a.waybillValueAdd.receiptFee || 0
			    const agentBusinessFee = +a.waybillValueAdd.agentBusinessFee || 0
			    const warehouseFee = +a.waybillValueAdd.warehouseFee || 0
			    const loadingFee = +a.waybillValueAdd.loadingFee || 0
			    const unloadFee = +a.waybillValueAdd.unloadFee || 0
			    const drayage = +a.waybillValueAdd.drayage || 0
			    const otherFee = +a.waybillValueAdd.otherFee || 0
		
			    this.formData.totalAmount = (totalCarriageFee + totalPackingFee + shipPickUpFee + deliveryPickUpFee + parcelInsuranceFee + receiptFee + agentBusinessFee + warehouseFee + loadingFee + unloadFee + drayage + otherFee).toFixed(2)
			    this.formData.invoiceTotal=this.formData.totalAmount
			},
			//确认开票
			invoiceDialogConfirm(){
				this.$refs.form.validate().then(res => {
					uni.showLoading({
						title: '正在开票...',
					})
					//开票完成
					invoiceAdd(this.formData).then(res => {
						uni.hideLoading()
						if (res.code == '200') {
							uni.showToast({
								title: '开票成功',
								icon: 'none'
							})
							this.$refs.invoiceDialog.close()
							this.$parent.getAwaitInvoiceList()  //调用父组件的getAwaitInvoiceList函数，重新加载数据
							//this.$forceUpdate(); //重新加载数据
						} else {
							uni.showToast({
								icon: 'none',
								title: res.msg
							})
						}	
					})
				}).catch(err => {
					uni.hideLoading()
					uni.showToast({
						title: err[0].errorMessage,
						icon: 'none'
					})
				})
				
			},
			//重新开票
			cancelInvoiceDialog(row){
				this.formData_cancel=row
				this.$refs.cancelInvoiceDialog.open()
			},
			//确认重新开票
			cancelInvoiceDialogConfirm(){
				this.$refs.form_cancel.validate().then(res => {
					uni.showLoading({
						title: '正在重新开票...',
					})
					reInvoice(this.formData_cancel).then(res => {
						uni.hideLoading()
						if (res.code == '200') {
							uni.showToast({
								title: '重新开票成功',
								icon: 'none'
							})
							this.$parent.getInvoiceRecord()  //调用父组件的getInvoiceRecord函数，重新加载数据
							//this.$forceUpdate(); //重新加载数据
						} else {
							uni.showToast({
								icon: 'none',
								title: res.msg
							})
						}	
					})
				}).catch(err => {
					uni.hideLoading()
					uni.showToast({
						title: err[0].errorMessage,
						icon: 'none'
					})
				})
				
			},
		},
		
	}
</script>

<style scoped>

</style>